package com.chixing.service.impl;



import com.chixing.mapper.CompanyDetailMapper;
import com.chixing.mapper.JobDetailMapper;
import com.chixing.mapper.MycollectsMapper;
import com.chixing.pojo.CompanyDetail;
import com.chixing.pojo.JobAndCompany;
import com.chixing.pojo.JobDetail;
import com.chixing.service.RecommentService;
import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.impl.model.jdbc.MySQLJDBCDataModel;
import org.apache.mahout.cf.taste.impl.neighborhood.NearestNUserNeighborhood;
import org.apache.mahout.cf.taste.impl.recommender.GenericItemBasedRecommender;
import org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender;
import org.apache.mahout.cf.taste.impl.similarity.CityBlockSimilarity;
import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity;
import org.apache.mahout.cf.taste.model.DataModel;
import org.apache.mahout.cf.taste.neighborhood.UserNeighborhood;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;
import org.apache.mahout.cf.taste.recommender.Recommender;
import org.apache.mahout.cf.taste.similarity.ItemSimilarity;
import org.apache.mahout.cf.taste.similarity.UserSimilarity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("recommtservice")
@Repository
public class RecommentServiceImpl implements RecommentService {
    @Autowired
    private MycollectsMapper mycollectsMapper;
    @Autowired
    private JobDetailMapper jobDetailMapper;
    @Autowired
    private CompanyDetailMapper companyDetailMapper;
@Autowired
private DataModel dataModel;


//    @Override
//    public List<JobAndCompany> getRecommentByCollect(Integer perId, Integer howMany) {
//        List<JobAndCompany> list=null;
//
//
////        try {
////            /*计算相似度，相似度的计算方式很多，采用基于皮尔逊相关性的相似度*/
////            UserSimilarity similarity=new PearsonCorrelationSimilarity(dataModel);
////           /*
////                计算最近邻居，邻居有两种算法：基于固定数量的邻居和基于相似度的邻居
////                这里采用基于固定数量的邻居
////            */
////            UserNeighborhood userNeighborhood=new NearestNUserNeighborhood(100,similarity,dataModel);
////            Recommender recommender=new GenericUserBasedRecommender(dataModel,userNeighborhood,similarity);
////            long starttime=System.currentTimeMillis();
////            /*推荐商品*/
////            List<RecommendedItem> recommendedItemList =  recommender.recommend(perId,howMany);
////            List<Integer> jobIds=new ArrayList<>();
////            for (RecommendedItem recommendedItem:recommendedItemList) {
////                System.out.println("recommendedItem:" + recommendedItem);
////                jobIds.add((int)recommendedItem.getItemID());
////            }
////            System.out.println("推荐出来的 商品的 id集合:" + jobIds);
////            /*根据职位id 查询商品*/
////            if (jobIds!=null&&jobIds.size()>0){
////                for (Integer jobId:jobIds){
////                    JobDetail jobDetail = jobDetailMapper.selectById(jobId);
////                    CompanyDetail companyDetail = companyDetailMapper.selectById(jobDetail.getCompanyId());
////                    JobAndCompany jobAndCompany=new JobAndCompany(jobDetail,companyDetail);
////                    list.add(jobAndCompany);
////                }
////
////            }
////            else{
////                list=new ArrayList<>();
////            }
////            System.out.println("推荐数量是：" + list.size()  );
////
////        } catch (TasteException e) {
////            e.printStackTrace();
////        }
//
//        try {
//            /*计算相似度，相似度的计算方式很多，采用基于曼哈顿距离算法*/
//            UserSimilarity similarity=new CityBlockSimilarity(dataModel);
//           /*
//                计算最近邻居，邻居有两种算法：基于固定数量的邻居和基于相似度的邻居
//                这里采用基于固定数量的邻居
//            */
//            UserNeighborhood userNeighborhood=new NearestNUserNeighborhood(5,similarity,dataModel);
//            Recommender recommender=new GenericUserBasedRecommender(dataModel,userNeighborhood,similarity);
//            long starttime=System.currentTimeMillis();
//            /*推荐商品*/
//            // 给用户perId推荐howmany个相似数据
//            List<RecommendedItem> recommendedItemList =  recommender.recommend(perId,howMany);
//            List<Integer> jobIds=new ArrayList<>();
//            for (RecommendedItem recommendedItem:recommendedItemList) {
//                System.out.println("recommendedItem:" + recommendedItem);
//                jobIds.add((int)recommendedItem.getItemID());
//            }
//            System.out.println("推荐出来的 商品的 id集合:" + jobIds);
//            /*根据职位id 查询商品*/
//            if (jobIds!=null&&jobIds.size()>0){
//                for (Integer jobId:jobIds){
//                    System.out.println("//////////////////////////////////////////////////////");
//                    System.out.println("---------------------**************************"+jobId);
//                    JobDetail jobDetail = jobDetailMapper.selectById(jobId);
//                    CompanyDetail companyDetail = companyDetailMapper.selectById(jobDetail.getCompanyId());
//                    JobAndCompany jobAndCompany=new JobAndCompany(jobDetail,companyDetail);
//                    list.add(jobAndCompany);
//                }
//
//            }
//            else{
//                System.out.println(",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,");
//                list=new ArrayList<>();
//            }
//            System.out.println("推荐数量是：" + list.size()  );
//
//        } catch (TasteException e) {
//            e.printStackTrace();
//        }
//        return list;
//    }

    @Override
    public List<JobAndCompany> getRecommentByCollectJob(Integer perId, Integer jobId, Integer howMany) {
        List<JobAndCompany> list=null;
        try {
            /*计算相似度，相似度的计算方式很多，采用基于曼哈顿距离*/
        ItemSimilarity similarity=new CityBlockSimilarity(dataModel);
            /*构建推荐器，基于用户的协同过滤推荐*/
        GenericItemBasedRecommender recommender=new GenericItemBasedRecommender(dataModel,similarity);
        long starttime=System.currentTimeMillis();
        List<Integer> jobIds=new ArrayList<>();
            /*推荐商品*/
        List<RecommendedItem> recommendedItemList=recommender.recommendedBecause(perId,jobId,howMany);
        for (RecommendedItem recommendedItem:recommendedItemList){
            System.out.println("recommendedItem"+recommendedItem);
            jobIds.add((int) recommendedItem.getItemID());
        }
            System.out.println("推荐出来的 职位的 id集合:" + jobIds);
            /*根据职位id 查询商品*/
            if (jobIds!=null&&jobIds.size()>0){
                list=new ArrayList<>();
                for (Integer id:jobIds){
                    System.out.println("//////////////////////////////////////////////////////");
                    System.out.println("---------------------**************************"+id);
                    JobDetail jobDetail = jobDetailMapper.selectById(id);
                    CompanyDetail companyDetail = companyDetailMapper.selectById(jobDetail.getCompanyId());
                    JobAndCompany jobAndCompany=new JobAndCompany(jobDetail,companyDetail);
                    list.add(jobAndCompany);
                }

            }
            else{
                System.out.println(",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,");
                list=new ArrayList<>();
            }
            System.out.println("推荐数量是：" + list.size()  + ",耗时：" + (System.currentTimeMillis()-starttime) );
        } catch (TasteException e) {
            e.printStackTrace();
        }
        return list;
    }
}
